package com.cleaningcloud.cld.service.impl.mini;

import com.cleaningcloud.cld.domain.CldMaterialAllocate;
import com.cleaningcloud.cld.domain.CldProjectRepository;
import com.cleaningcloud.cld.domain.dto.CldMaterialAllocateDto;
import com.cleaningcloud.cld.domain.dto.CldMaterialConsumeDto;
import com.cleaningcloud.cld.domain.mini.MySupplies;
import com.cleaningcloud.cld.domain.vo.CldMaterialAllocateVo;
import com.cleaningcloud.cld.domain.vo.CldTeamMemberDetailsListVo;
import com.cleaningcloud.cld.domain.vo.CldTeamMemberDetailsVo;
import com.cleaningcloud.cld.mapper.CldMaterialAllocateMapper;
import com.cleaningcloud.cld.mapper.CldProjectRepositoryMapper;
import com.cleaningcloud.cld.mapper.mini.MiniMaterialMapper;
import com.cleaningcloud.cld.service.ICldMaterialAllocateService;
import com.cleaningcloud.cld.service.ICldMaterialConsumeService;
import com.cleaningcloud.cld.service.mini.IMiniMaterialService;
import com.cleaningcloud.common.core.constant.SecurityConstants;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.api.RemoteWareService;
import com.cleaningcloud.erp.api.domain.ErpWareHouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class MiniMaterialServiceImpl implements IMiniMaterialService {


    @Autowired
    private ICldMaterialAllocateService cldMaterialAllocateService;

    @Autowired
    private CldMaterialAllocateMapper cldMaterialAllocateMapper;

    @Autowired
    private MiniMaterialMapper miniMaterialMapper;

    @Autowired
    private RemoteWareService remoteWareService;

    @Autowired
    private ICldMaterialConsumeService cldMaterialConsumeService;

    @Autowired
    private CldProjectRepositoryMapper repositoryMapper;

    @Override
    @Transactional
    public int materialsIssue(CldMaterialAllocateDto cldMaterialAllocateDto) {
        // 根据项目ID查询项目仓库id
        ErpWareHouse materialOut = remoteWareService.getWareHouseByProjectId(cldMaterialAllocateDto.getProjectId(), SecurityConstants.INNER);
        if (materialOut == null) {
            throw new RuntimeException("项目仓库查询失败");
        }
        if (StringUtils.isNotEmpty(cldMaterialAllocateDto.getMaterialInIds())) {
            for (String s : cldMaterialAllocateDto.getMaterialInIds().split(",")) {
                cldMaterialAllocateDto.setMaterialInPerson(SecurityUtils.getUserId());
                cldMaterialAllocateDto.setMaterialInId(Long.valueOf(s));
                cldMaterialAllocateDto.setMaterialOutId(materialOut.getWareId());
                cldMaterialAllocateDto.setMaterialOutPerson(SecurityUtils.getUserId());
                cldMaterialAllocateDto.setAllocateType(1L);
                cldMaterialAllocateDto.setRemark("物资下发");
                cldMaterialAllocateDto.setMaterialAllocateDate(DateUtils.getNowDate());
                cldMaterialAllocateDto.setIsConfirm(0L);
                cldMaterialAllocateService.insertCldMaterialAllocate(cldMaterialAllocateDto);
            }
        } else {
            throw new RuntimeException("物资下发仓库不能为空！");
        }
        return 1;
    }

    @Override
    public int materialsRevocation(Long materialAllocateId) {
        // 是否已确认
        CldMaterialAllocateVo cldMaterialAllocateVo = cldMaterialAllocateMapper.selectCldMaterialAllocateByMaterialAllocateId(materialAllocateId);
        if (cldMaterialAllocateVo == null) {
            throw new RuntimeException("物资下发记录查询失败！");
        }
        if (cldMaterialAllocateVo.getIsConfirm() == 1 || cldMaterialAllocateVo.getApproveStatus() == 1) {
            throw new RuntimeException("已确认领用,无法撤销！");
        }
        cldMaterialAllocateMapper.deleteWareAllocateDetailsByWareAllocateId(materialAllocateId);
        cldMaterialAllocateMapper.deleteCldMaterialAllocateByMaterialAllocateId(materialAllocateId);
        return 1;
    }

    @Override
    public int materialsConfirmCollection(Long materialAllocateId) {
        CldMaterialAllocate cldMaterialAllocate = cldMaterialAllocateMapper.selectCldMaterialAllocateByMaterialAllocateId(materialAllocateId);
        if (cldMaterialAllocate == null) {
            throw new RuntimeException("领用记录查询失败");
        }
        cldMaterialAllocate.setIsConfirm(1L);
        cldMaterialAllocate.setConfirmTime(DateUtils.getNowDate());
        if (cldMaterialAllocate.getApproveStatus() == 0) {
            // 同时审核通过
            cldMaterialAllocate.setApproveStatus(1L);
            cldMaterialAllocateService.auditCldMaterialAllocate(cldMaterialAllocate);
        }
        return cldMaterialAllocateMapper.updateCldMaterialAllocate(cldMaterialAllocate);
    }

    @Override
    public int materialsConsume(CldMaterialConsumeDto cldMaterialConsumeDto) {
        if (cldMaterialConsumeDto.getConsumeType() == 2) {
            // 根据项目ID查询项目仓库id
            ErpWareHouse materialOut = remoteWareService.getWareHouseByProjectId(cldMaterialConsumeDto.getProjectId(), SecurityConstants.INNER);
            if (materialOut == null) {
                throw new RuntimeException("项目仓库查询失败");
            }
            cldMaterialConsumeDto.setConsumeWareId(materialOut.getWareId());
        }
        return cldMaterialConsumeService.insertCldMaterialConsume(cldMaterialConsumeDto);
    }

    @Override
    public List<Map<String, Object>> selectSuppliesConsumeList(Map<String, Object> map) {
        return miniMaterialMapper.selectSuppliesConsumeList(map);
    }

    @Override
    public List<Map<String, Object>> selectDaySuppliesConsumeList(Map<String, Object> map) {
        return miniMaterialMapper.selectDaySuppliesConsumeList(map);
    }

    @Override
    public BigDecimal getSuppliesConsumeMonthlyTotal(Map<String, Object> map) {
        return miniMaterialMapper.getSuppliesConsumeMonthlyTotal(map);
    }

    @Override
    public List<MySupplies> selectMySupplies(Long projectPostId) {
        return miniMaterialMapper.selectMySupplies(projectPostId);
    }

    @Override
    public List<CldTeamMemberDetailsVo> getEmployeeInventory(Long projectId) {
        // 小组明细
        List<CldTeamMemberDetailsVo> memberDetailsVos = repositoryMapper.selectCldTeamMemberDetailsList(projectId);
        // 小组消耗明细
        for (CldTeamMemberDetailsVo memberDetailsVo : memberDetailsVos) {
            for (CldTeamMemberDetailsListVo cldTeamMemberDetailsListVo : memberDetailsVo.getList()) {
                CldProjectRepository cldProjectRepository = new CldProjectRepository();
                cldProjectRepository.setWareId(cldTeamMemberDetailsListVo.getWareId());
                Map<String, Object> map = repositoryMapper.selectConsumeByWareId(cldProjectRepository);
                if (map != null) {
                    cldTeamMemberDetailsListVo.setConsumeNumber(Integer.parseInt(map.get("consumeNumber").toString()));
                    cldTeamMemberDetailsListVo.setConsumeMoney(map.get("consumeMoney").toString());
                }
            }
        }
        return memberDetailsVos;
    }

    @Override
    public List<Map<String, Object>> getEmployeeInventoryByCommodityId(Map<String, Object> map) {
        return miniMaterialMapper.getEmployeeInventoryByCommodityId(map);
    }
}
